﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public partial class improveindex : System.Web.UI.Page
{
        private string title;
        private string path;
        private string text;
        private string sort;
        
    protected void Page_Load(object sender, EventArgs e)
    {
        
        title = Application["title"].ToString();
        path = Application["path"].ToString();
        Text_Title.Text = title;

        //从数据库中提取sort属性
        SqlConnection sc3 = new SqlConnection(Application["sqlconnection"].ToString());
        sc3.Open();
        SqlCommand com_getsort = sc3.CreateCommand();
        com_getsort.CommandText = "select Sort from Suoyin where Title = @title";
        com_getsort.Parameters.Add("@title",SqlDbType.Text);
        com_getsort.Parameters["@title"] = new SqlParameter("@title", title);
        sort=com_getsort.ExecuteScalar().ToString();
        Text_Sort.Text = sort;
        sc3.Close();

        using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory+path, System.Text.Encoding.Default))
        {
            sr.ReadLine();
            sr.ReadLine();
            text = sr.ReadToEnd();
        }
        CKEditorControl1.FontSizes = "large";
        CKEditorControl1.Value = text;
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ctitle =title+DateTime.Now.ToLongDateString()+DateTime.Now.ToLongTimeString().Replace(":","s")+ ".txt";
        MessageBox.Show(path);
        string cpath = AppDomain.CurrentDomain.BaseDirectory + "\\index_improved\\" + ctitle;
        try
        {    
            
            //添加简易文本编辑器
            FileStream aFile = new FileStream(cpath, FileMode.CreateNew);
            StreamWriter write = new StreamWriter(aFile);
            write.Write("<title>" + Text_Title.Text + "</title>");
            write.WriteLine();
            write.Write("<sort>" + Text_Sort.Text + "</sort>");
            write.WriteLine();
            write.Write(DateTime.Now.ToString());
            write.WriteLine();
            write.Write(CKEditorControl1.Value); write.Close();
            //Response.Write("<script language=javascript>alert=('索引已成功提交，请等待审核！')</script>");
            
            Response.Redirect("index.aspx");

        }
        catch (IOException ee)
        {
            //string mes = ee.StackTrace;
            //Response.Write("<script language=javascript>alert=(mes)</script>"); 
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx",true);
    }
}